package com.tyx.modules.manage.controller;

import java.util.AbstractCollection;
import java.util.Arrays;
import java.util.Date;
import java.util.Map;

import com.tyx.common.exception.ServiceException;
import com.tyx.modules.sys.controller.AbstractController;
import com.tyx.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.tyx.modules.manage.entity.OperationEntity;
import com.tyx.modules.manage.service.OperationService;
import com.tyx.common.utils.PageUtils;
import com.tyx.common.utils.R;



/**
 * 
 *
 * @author wangchao
 * @email 
 * @date 2019-06-06 09:51:05
 */
@RestController
@RequestMapping("manage/operation")
public class OperationController extends AbstractController {
    @Autowired
    private OperationService operationService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("manage:operation:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = operationService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("manage:operation:info")
    public R info(@PathVariable("id") Integer id){
        OperationEntity operation = operationService.getById(id);

        return R.ok().put("operation", operation);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("manage:operation:save")
    public R save(@RequestBody OperationEntity operation){
        operation.setCreateTime(new Date());//设置提交时间为当前系统时间
        operation.setCreateUser(this.getUserId());//获取当前登陆用户id
        operation.setState(1);
        operationService.save(operation);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("manage:operation:update")
    public R update(@RequestBody OperationEntity operation){
        ValidatorUtils.validateEntity(operation);
        operation.setUpdateTime(new Date());//获取当前系统时间
        operation.setUpdateUser(this.getUserId());//获取当前登陆用户id
        operationService.updateById(operation);
        return R.ok();
    }

    /**
     * 公司禁用
     * @return
     */
    @RequestMapping("/stopUsing")
    @RequiresPermissions("manage:operation:stopUsing")
    public R opProhibit(Long opId){
        if (opId == null){
            return R.error(10021,"参数异常");
        }
        try{
            this.operationService.stopUsing(opId,this.getUserId());
            return R.ok();
        }catch (ServiceException ex){
            return R.error(ex.getErrorCode(),ex.getErrorMsg());
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("manage:operation:delete")
    public R delete(@RequestBody Integer[] ids){
        operationService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
